package com.jason.digest.utils;

import org.springframework.util.ObjectUtils;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

public class MapSortUtils {

    public static Map<String, Object> sortMapByKey(Map<String, Object> map) {
        //判断是否为空
        if (ObjectUtils.isEmpty(map)) {
            throw new RuntimeException("输入参数为空");
        }
        //排序
        Map<String, Object> sortMap = new TreeMap<>(new MyMapComparator());
        sortMap.putAll(map);
        return sortMap;
    }

    //排序
    static class MyMapComparator implements Comparator<String> {
        @Override
        public int compare(String o1, String o2) {
            return o1.compareTo(o2);
        }
    }
}
